package vietnam.traveltools.adapter;

import java.util.List;

import vietnam.traveltools.variables.ListItem;
import vietnam.traveltools.view.TaxiView;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.DataSetObservable;
import android.database.DataSetObserver;
import android.net.Uri;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.SpinnerAdapter;

public class TaxiAdapter extends ArrayAdapter<ListItem> implements ListAdapter,
		SpinnerAdapter {
	private final DataSetObservable mDataSetObservable = new DataSetObservable();
	private List<ListItem> _listItems;
	private Context _context;

	public TaxiAdapter(Context context, int textViewResourceId,
			List<ListItem> objects) {
		super(context, textViewResourceId, objects);
		this._context = context;
		this._listItems = objects;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		TaxiView view = new TaxiView(this._context);
		
		view.setListItem(this._listItems.get(position));
		final String tvPhone = view.tvPhone.getText().toString().substring(4).trim().replace("(", "").replace(")", "");
		
		view.lnTaxi.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
				
				AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext());
				 alertDialog.setTitle("Bạn muốn gọi?");
				 alertDialog.setMessage("Bạn hãy chắc chắn rằng việc này được thực hiện với mục đích thật sự");
				 alertDialog.setPositiveButton("Gọi",
				 new DialogInterface.OnClickListener() {
				 public void onClick(DialogInterface dialog,int which) {
//				 Toast.makeText(getContext(), "Bạn đã chọn YES", Toast.LENGTH_SHORT).show();
					 	Intent callIntent = new Intent(Intent.ACTION_CALL);
						callIntent.setData(Uri.parse("tel:"+tvPhone));
						getContext().startActivity(callIntent);
				 }
				 });
				 alertDialog.setNegativeButton("Không",
				 new DialogInterface.OnClickListener() {
				 public void onClick(DialogInterface dialog, int which) {
				 dialog.cancel();
				 }
				 });
				 alertDialog.show();
				 
				
//				Toast.makeText(getContext(), tvPhone, Toast.LENGTH_SHORT).show();3
				
			}
		});
		return view;
	}
	
	
	

	
	public void notifyDataSetChanged() {
		mDataSetObservable.notifyChanged();
	}

	public boolean hasStableIds() {
		return false;
	}

	public void registerDataSetObserver(DataSetObserver observer) {
		mDataSetObservable.registerObserver(observer);
	}

	public void unregisterDataSetObserver(DataSetObserver observer) {
		mDataSetObservable.unregisterObserver(observer);
	}

	/**
	 * Notifies the attached observers that the underlying data is no longer
	 * valid or available. Once invoked this adapter is no longer valid and
	 * should not report further data set changes.
	 */
	public void notifyDataSetInvalidated() {
		mDataSetObservable.notifyInvalidated();
	}

	public boolean areAllItemsEnabled() {
		return true;
	}

	public boolean isEnabled(int position) {
		return true;
	}

	public View getDropDownView(int position, View convertView, ViewGroup parent) {
		return getView(position, convertView, parent);
	}

	public int getItemViewType(int position) {
		return 0;
	}

	public int getViewTypeCount() {
		return 1;
	}

	public boolean isEmpty() {
		return getCount() == 0;
	}

}
